clear; clc; close all;

alpha = linspace(-6, 6, 400);
eta = linspace(-6, 6, 400);
[Alpha, Eta] = meshgrid(alpha, eta);

F1 = Alpha .* exp(-Eta) - 1;
F2 = Alpha .* exp(-Eta) - 9;
F3 = -0.5 * Alpha.^2 .* exp(-2*Eta) - 3.25 * Alpha .* exp(-Eta) + 5.75;

figure;
hold on;
box on;

% 先用plot画示意线，作为图例句柄
hRed = plot(nan, nan, 'r-', 'LineWidth', 1.5);
hBlue = plot(nan, nan, 'b-', 'LineWidth', 1.5);
hGreen = plot(nan, nan, 'g-', 'LineWidth', 1.5);

% 绘制隐式曲线，颜色也用红蓝绿，但不用于图例
contour(Alpha, Eta, F1, [0 0], 'LineWidth', 1.5, 'LineColor', 'r');
contour(Alpha, Eta, F2, [0 0], 'LineWidth', 1.5, 'LineColor', 'b');
contour(Alpha, Eta, F3, [0 0], 'LineWidth', 1.5, 'LineColor', 'g');

xlim([-6 6]);
ylim([-6 6]);
xlabel('\alpha', 'FontSize', 12);
ylabel('\eta', 'FontSize', 12);
title('Fig.2(a) Stability boundaries of mCNN model');
set(gca, 'FontSize', 12);

legend([hRed, hBlue, hGreen], ...
    {'\alphae^{-\eta} - 1 = 0', ...
     '\alphae^{-\eta} - 9 = 0', ...
     '-0.5 \alpha^2e^{-2\eta} - 3.25 \alphae^{-\eta} + 5.75 = 0'}, ...
    'Location', 'best', 'FontSize', 12);

hold off;

function dx = mCNN(x, alpha)
    A = [-alpha, 0, 0;
          0,     0, 0;
          0,     0, 0];
    B = [1, -4, -3.5;
         0,  1,  2;
        -1, -4, 1.5];
    X = x(1:3);
    Q = x(4:6);
    M = exp(-Q);
    Y = M .* X;
    dXdt = -X + A*Y + B*X;
    dQdt = X;
    dx = [dXdt; dQdt];
end